package com.pn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pn.entity.OutStore;
import com.pn.entity.ReturnGoods;
import com.pn.mapper.InstoreMapper;
import com.pn.mapper.ProductMapper;
import com.pn.mapper.ReturnMapper;
import com.pn.service.ReturnService;
import com.pn.utils.PageBean;
import com.pn.utils.Result;
import com.pn.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.Date;

@Service
public class RetuernServiceImpl implements ReturnService {

    @Autowired
    private ReturnMapper returnMapper;
    @Autowired
    private InstoreMapper instoreMapper;
    @Autowired
    private ProductMapper productMapper;

    //退货列表/return-page-list
    @Override
    public PageBean<ReturnGoods> returnPageList(ReturnGoods returnGoods) {
        PageHelper.startPage(returnGoods.getPageNum(), returnGoods.getPageSize());
        Page<ReturnGoods> page = returnMapper.selectReturnList(returnGoods);

        return new PageBean<>(page.getTotal(),page.getResult());
    }

    //添加退货return-add
    @Override
    public Result saveReturn(ReturnGoods returnGoods) {
        //查询库存是否能够退货
        int quantity = productMapper.findInventById(returnGoods.getReturnId());
        if (quantity < returnGoods.getReturnQuantity()){
            return Result.err(Result.CODE_ERR_BUSINESS,"商品的库存不足以退货");
        }

        //获取修改人id
        Map<String,Object> map =(Map<String, Object>) ThreadLocalUtil.get();
        Integer userId =(Integer) map.get("userId");
        returnGoods.setUserId(userId);

        //生成单据编号
        // 获取当前日期，格式为yyyyMMdd
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String datePart = dateFormat.format(new Date());

        // 生成一个随机序列号，这里假设序列号是4位数字
        int randomPart = ThreadLocalRandom.current().nextInt(1000, 9999);

        // 拼接日期和序列号，形成单据编号
        // 这里假设单据编号总长度不超过14位
        String documentNumber = "D" + datePart + String.format("%04d", randomPart);
        returnGoods.setDocumentNumber(documentNumber);

        int i = returnMapper.insertOutStore(returnGoods);
        if (i > 0){
            return Result.ok("添加退货单成功");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"添加退货单失败");
    }



    //确认退货return-confirm
    @Override
    @Transactional
    public Result returnConfirm(ReturnGoods returnGoods) {
       //把退货状态修改为已退货
        int i = returnMapper.setIsReturnById(returnGoods.getReturnId());
        if (i > 0){
            //把商品表中库存数量 -returnGoods.getReturnQuantity()
            int j = instoreMapper.setInventById(returnGoods.getProductId(),-returnGoods.getReturnQuantity());
            if (j>0){
                return Result.ok("确认退货成功！");
            }
            return Result.err(Result.CODE_ERR_BUSINESS,"确认退货失败！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"确认退货失败！");
    }
}
